package com.app.balanceclient;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * 加权轮询客户端
 */
public class RoundRobinClient {

    public class Node {
        private String ip;

        private Integer weight;

        private Integer currentWeight;

        public Node(String ip, Integer weight) {
            this.ip = ip;
            this.weight = weight;
            this.currentWeight = weight;
        }

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public Integer getWeight() {
            return weight;
        }

        public void setWeight(Integer weight) {
            this.weight = weight;
        }

        public Integer getCurrentWeight() {
            return currentWeight;
        }

        public void setCurrentWeight(Integer currentWeight) {
            this.currentWeight = currentWeight;
        }
    }

    List<Node> servers = Arrays.asList(new Node("192.168.1.1", 1), new Node("192.168.1.2", 2), new Node("192.168.1.3", 3), new Node("192.168.1.4", 4));
    private final Integer totalWeight;

    public RoundRobinClient() {
        this.totalWeight = servers.stream().mapToInt(Node::getWeight).reduce(Integer::sum).getAsInt();
    }

    public String getServer() {
        Node node = servers.stream().max(Comparator.comparingInt(Node::getCurrentWeight)).get();
        node.setCurrentWeight(node.getCurrentWeight() - totalWeight);
        servers.forEach(server -> server.setCurrentWeight(server.getCurrentWeight() + server.getWeight()));
        return node.getIp();
    }


    public static void main(String[] args) {
        RoundRobinClient roundRobinClient = new RoundRobinClient();
        for (int i = 0; i < 20; i++) {
            String server = roundRobinClient.getServer();
            System.out.println("select server: " + server);
        }
    }
}